/**
 * Private Variable Declaration
 * */
 import Modules.Activity.ActivityDetail;
 import Modules.NavigationDetails;
 import Modules.NavigationInfo;
 import Modules.PostLogin;
 import Modules.Windows.CMDWindow;
 
 import com.desidea.common.CommonClass;
 import com.desidea.common.MenuConstants;
 import com.desidea.components.grid.SuperGridEvent;
 import com.desidea.components.grid.button.SuperGridButton;
 import com.desidea.components.spinner.BusySpinner;
 import com.desidea.webservice.PartnerService;
 import com.desidea.webservice.ProcessServiceRequestResultEvent;
 
 import flash.events.MouseEvent;
 
 import mx.controls.Alert;
 import mx.controls.Button;
 import mx.core.Application;
 import mx.rpc.events.FaultEvent;



private var _ni:NavigationInfo;
public function get NavInfo():NavigationInfo{
	return _ni;
}
public function set NavInfo(value:NavigationInfo):void{
	_ni = value;
}
 

/**
 * Event Handling - CreationComplete event
 * */
private function windowCreated():void{
	
	BusySpinner.getInstance().play(Application.application.ModuleCvs);
	if(CommonClass.UserLoginInfo.CanCreateActivity){
		var newItemBtn:Button = new Button();
		newItemBtn.label = "Create New Activity";
		newItemBtn.addEventListener(MouseEvent.CLICK, addNew);
		myActivitiesGrid.toolBar.addChild(newItemBtn);
	}
	
	this.myActivitiesGrid.addEventListener(SuperGridEvent.CLICKED, gridButtonColumnClicked);

	if(this.NavInfo == null)
		loadData();
	else if(this.NavInfo != null && this.NavInfo.destinationNavObj.pkID == null){
		loadData();
	}else{
		showDetail( SuperGridButton.MODE_VIEW, this.NavInfo.destinationNavObj.pkID, this.NavInfo.sourceNavObj.pkID);
	}
}
private function addNew(event:MouseEvent):void{
	var src:NavigationDetails = new NavigationDetails(MenuConstants.CMD, MenuConstants.MY_ACTIVITIES);
	var dst:NavigationDetails = new NavigationDetails(MenuConstants.CMD, MenuConstants.MY_ACTIVITIES,"",SuperGridButton.MODE_EDIT);
	var ni:NavigationInfo = new NavigationInfo(src,dst);
	ni.previousLink = this.NavInfo.copy();
	(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
}
private function loadData():void{
	var params:String = "OPERATION=ACTIVITY_LIST" +
						"&USERID="+CommonClass.UserID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForMyActivitiesList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
}

private function callForMyActivitiesList(event:ProcessServiceRequestResultEvent):void{
	var response:XML = XML(event.result);
	lblError.text = "";
	if(response.RESPONSE.RESULT == "E"){
		lblError.text = response.RESPONSE.MESSAGE
	}else if(response.RESPONSE.RESULT == 'S'){
		this.myActivitiesGrid.dataProvider = response;
	}
	setColumnWidths();
	BusySpinner.getInstance().stop();	
}

private function partnerServiceFaultHandler(event:FaultEvent):void{
	BusySpinner.getInstance().stop();
	Alert.show(event.fault.toString());
}

private function setColumnWidths():void{
	CommonClass.setColumnWidth(this.myActivitiesGrid,1, 90, "right");
	CommonClass.setColumnWidth(this.myActivitiesGrid,2, 150);
	CommonClass.setColumnWidth(this.myActivitiesGrid,3, 150);
	CommonClass.setColumnWidth(this.myActivitiesGrid,4, 100);
	CommonClass.setColumnWidth(this.myActivitiesGrid,5, 90);
	CommonClass.setColumnWidth(this.myActivitiesGrid,6, 130);
	CommonClass.setColumnWidth(this.myActivitiesGrid,7, 100);
}

private function gridButtonColumnClicked(event:SuperGridEvent):void{
	BusySpinner.getInstance().play(Application.application.ModuleCvs);
	handleGridButtonClick(event);
}

private function handleGridButtonClick(event:SuperGridEvent, moduleCode:String = null):void{
	var mode:String =  String(event.data.linkId).toLowerCase();
	
	if(mode == SuperGridButton.MODE_EDIT || mode == SuperGridButton.MODE_VIEW || mode == SuperGridButton.MODE_VIEWICON){
		var src:NavigationDetails = new NavigationDetails(MenuConstants.CMD, MenuConstants.MY_ACTIVITIES,null);
		var dst:NavigationDetails = new NavigationDetails(MenuConstants.CMD,MenuConstants.MY_ACTIVITIES,event.data.data["key"].toString());
		var ni:NavigationInfo = new NavigationInfo(src,dst);
		ni.previousLink = this.NavInfo;
		(Application.application.ModuleCvs.getChildAt(0) as PostLogin).doNavigate(ni);
	}else{
		BusySpinner.getInstance().stop();
		Alert.show("Mode : " + mode + " not handled");
	}
	
}

private function showDetail(mode:String, key:String="", accountGUID:String=""):void{

//	if(key=="") mode = SuperGridButton.MODE_EDIT; 

	var win:CMDWindow = this.parentDocument as CMDWindow;

	var ad:ActivityDetail = new ActivityDetail();
	ad.mode = win.data.toString().toLowerCase();
	ad.key = key;

	ad.mode = this.NavInfo == null?SuperGridButton.MODE_VIEW:this.NavInfo.destinationNavObj.mode;
	ad.accountGUID = accountGUID;
	ad.NavInfo = (this.NavInfo == null)?null : this.NavInfo.copy();

	win.removeAllChildren();
	win.addChild(ad);
}